package ldkxingzhe.animation.chapter4;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;

import ldkxingzhe.animation.R;

/**
 * Created by xingzhe on 15/11/3.
 */
public class InteractiveBookActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = InteractiveBookActivity.class.getSimpleName();

    // MARK: UI Elements
    ViewFlipper viewFlipper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chapter4_activity_interactive_book);
        viewFlipper = (ViewFlipper)findViewById(R.id.chapter4_view_flipper);
        findViewById(R.id.chapter4_btn_left).setOnClickListener(this);
        findViewById(R.id.chapter4_btn_right).setOnClickListener(this);

        View view = findViewById(R.id.chapter4_image_first);
        View secondView = findViewById(R.id.chapter4_image_second);

        AnimationSet animationSet = new AnimationSet(true);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 1.0f,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0);
        ScaleAnimation scaleAnimation = new ScaleAnimation(10,1,10,1,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
//        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(translateAnimation);
        animationSet.setDuration(2000);
        viewFlipper.setInAnimation(animationSet);

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "TranslationX", 0, 400);
        objectAnimator.setDuration(2000);
        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        objectAnimator.start();

        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 40);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Log.v(TAG,"onAnimationUpdate");
            }
        });
        valueAnimator.setDuration(2000);
        valueAnimator.setRepeatMode(ValueAnimator.INFINITE);
        valueAnimator.setRepeatCount(ValueAnimator.REVERSE);
        ValueAnimator.setFrameDelay(500);
        valueAnimator.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.chapter4_btn_left:
                viewFlipper.showPrevious();
                break;
            case R.id.chapter4_btn_right:
                viewFlipper.showNext();
                break;
            default:
                throw new IllegalStateException("The id is not know");
        }
    }
}
